<?php

class messages extends Controller
{
	
	public function index () {
		$this->session();
		$data['title'] = 'Messages';
		$model = new pm();
		$messages = $model->getInbox($_SESSION['stud_no']);
		$inbox = $model->countInbox($_SESSION['stud_no']);
		
		$data['content'] = new BufferedView('members/messages',array(
			"inbox" => $inbox,
			"messages" => $messages
		) );
		
		new View ("members/template", $data);
	}

   public function showMessage ($messageId) {
	
		$this->session();
		$data['title'] = new BufferedView('members/tviewMessage');
		$model = new pm();
		$inbox = $model->countInbox($_SESSION['stud_no']);
		$messagebyId = $model->getMessagebyID($_SESSION['stud_no'],$messageId); 
		$data['content'] = new BufferedView('members/viewMessage',array(
			"inbox" => $inbox,	
			 'message' => $messagebyId[0]
		) );
        
		
		new View ("members/membersTemplate", $data);
	}

	public function createMessage() {
		$this->session();

		
		$data['title'] = 'Members Page: Create Message';
		$model = new pm();
		$inbox = $model->countInbox($_SESSION['stud_no']);
		if( func_num_args() > 0)	$sender = func_get_arg(0);
		else	$sender = "";
		$data['content'] = new BufferedView('members/createMessage',array(
		"inbox" => $inbox,
		 "receiver" => $sender
		));
		
		new View ("members/template", $data);
	}

	public function send() {
		$this->session();
		$model = new pm();
		$date = gmdate ( 'YmdHis' );
		$createMessage = $model->insertPm($_SESSION['stud_no'], $_POST['to'], $date, $_POST['subject'] , $_POST['message'] ); 
		$message = 'Your message has been sent. Click <a href="/member/messages/createMessage">here</a> to go back.' ;
		new View('emptyTemplate', array( 'message' => $message ) );
	}

	public function sent() {
		$this->session();
		$data['title'] = new BufferedView('members/tsentMessage');
		$model = new pm();
		$messages = $model->getSent($_SESSION['stud_no']);
		$inbox = $model->countInbox($_SESSION['stud_no']);
		
		$data['content'] = new BufferedView('members/sentMessage',array(
		"inbox" => $inbox,
		"messages" => $messages
			
		) );
		new View ("members/template", $data);
	}
	
	public function showSentMessage ($messageId) {
	
		$this->session();
		$data['title'] = new BufferedView('members/tviewSentMessage');
		$model = new pm();
		$messagebyId = $model->getsentById($_SESSION['stud_no'],$messageId);
		$inbox = $model->countInbox($_SESSION['stud_no']);
		$data['content'] = new BufferedView('members/viewSentMessage',array(
			 "inbox" => $inbox,
			 'message' => $messagebyId[0]
		) );
        
		
		new View ("members/template", $data);
	}

	public function delete() {
		$this->session();
		// pag confirm na
		if( func_num_args() > 0 ) {
			if( func_get_arg(0) == 'confirm' ) {
				if( isset( $_POST['yes'] ) ) {
					$pm = new pm();
					$pm->deletePMs( $_POST['msgids'] );
					if( $_POST['referrer'] == 'inbox' ) $message = 'Your message has been deleted. Click <a href="/member/messages">here</a> to go back to inbox.' ;
					if( $_POST['referrer'] == 'sent' ) $message = 'Your message has been deleted. Click <a href="/member/messages/sent">here</a> to go back to sent messages.';
					new View('emptyTemplate', 
						array( 'message' => $message ) 
					);
				}
				if( isset( $_POST['no'] ) ) {
					header('Location: /member/messages'); // url na babalikan pag pinindot ang no
				}
			}
		}
		
		// pag wala pang confirm
		else {
			if( $_POST['referrer'] == 'inbox' ) $hiddenVars['referrer'] = 'inbox';
			if( $_POST['referrer'] == 'sent' ) $hiddenVars['referrer'] = 'sent';
			$hiddenVars['msgids'] = $_POST['msgids'];
			$confirmVars['url'] = '/member/messages/delete/confirm'; 
			$confirmVars['message'] = 'Are you sure you want to delete these messages?';
			$confirmVars['hiddenVars'] = $hiddenVars;
			$confirmForm = new BufferedView('confirmForm', $confirmVars);
			
			new View(
				'members/membersTemplate',
				array(
					'title' => new BufferedView('tConfirmForm'),
					'content' => $confirmForm
				)
			);
		}
		
	}

	public function action ()
	{
		if ($_POST['action'] === "Delete")
		 $this->delete();
		if ($_POST['action'] === "Reply")
		 $this->createMessage($_POST['sender']);

	}
		
	
}

